package com.xbongbong.saas.service;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.SheetTypeEnum;
import com.xbongbong.paas.pojo.FieldAttrEntityForImport;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.dto.ImportFormDataDTO;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.pojo.imports.CellTitlePojo;
import com.xbongbong.paas.pojo.imports.RowGroupPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.businessproduct.pojo.dto.BusinessProductListDTO;
import com.xbongbong.pro.customer.pojo.dto.ChangeStatusDTO;
import com.xbongbong.pro.detailtab.dto.ChangeDocumentStatusSaveDTO;
import com.xbongbong.pro.detailtab.vo.ChangeDocumentStatusSaveVO;
import com.xbongbong.pro.detailtab.vo.ProductSummaryVO;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.formdata.pojo.AfterSavePojo;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainBatchLinkItemDTO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainInLinkItemDTO;
import com.xbongbong.pro.listbatch.pojo.dto.AddLabelDTO;
import com.xbongbong.pro.product.pojo.ProductSavePojo;
import com.xbongbong.pro.product.pojo.dto.PagingProductDTO;
import com.xbongbong.pro.product.pojo.vo.PagingProductVO;
import com.xbongbong.pro.purchase.pojo.dto.PurchaseDeleteBatchDTO;
import com.xbongbong.pro.purchase.pojo.dto.PurchaseEditAttrUpdateDTO;
import com.xbongbong.pro.purchase.pojo.dto.PurchaseListDTO;
import com.xbongbong.pro.purchase.pojo.dto.PurchaseUpdateBatchDTO;
import com.xbongbong.pro.purchase.pojo.dto.PurchaseUpdateByBusinessRuleDTO;
import com.xbongbong.pro.purchase.pojo.vo.PurchaseDeleteBatchVO;
import com.xbongbong.pro.purchase.pojo.vo.PurchaseListVO;
import com.xbongbong.pro.purchase.pojo.vo.PurchaseUpdateBatchVO;
import com.xbongbong.pro.sheet.pojo.dto.SheetDeleteHandleRelateDTO;
import com.xbongbong.saas.domain.entity.PurchaseProductEntity;
import com.xbongbong.saas.domain.entity.ext.PurchaseEntityExt;
import com.xbongbong.saas.enums.UpdateDataTypeEnum;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 采购合同联系人service接口层
 * @author Jian.Wang
 * @date 2019/1/23 20:27
 * @since v1.0
 * @version v1.0
 */
public interface PurchaseService {
    /**
     * 采购合同保存前saas特殊校验
     *
     * @param validateDataDTO saas特殊校验数据入参
     * @author Jian.Wang
     * @date 2019/1/23 20:00
     * @since v1.0
     * @throws XbbException XbbException
     **/
    void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException;

    /**
     * 采购合同保存后逻辑
     *
     * @param saasFormSaveDTO saas保存数据
     * @author Jian.Wang
     * @date 2019/1/23 20:00
     * @since v1.0
     * @throws XbbException XbbException
     **/
    AfterSavePojo afterSave(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;


    /**
     * 保存前要处理的关联产品和采购合同入库信息
     * @param purchaseEntityExt  采购合同参数
     * @date 2019/2/27 14:53
     * @throws XbbException 异常
     * @author Jian.Wang
     */
    void dealWithPurchaseProduct(PurchaseEntityExt purchaseEntityExt, boolean b,Integer taskType) throws XbbException;
    /**
     * 获取采购合同剩余未入库产品信息及数量
     *
     * @param corpid 公司id
     * @param purchaseId 采购合同id
     * @param purchaseEntityExt 此参数可以为null ，用于更新关联出库单id，更安全
     * @param instockIdList 此参数用来去除特定入库单，显示采购合同剩余入库产品，用于编辑入库单使用，可以为null
     * @return 采购合同未入库产品列表
     */
    List<PurchaseProductEntity> getSurplusProduct(String corpid, Long purchaseId, PurchaseEntityExt purchaseEntityExt, List<Long> instockIdList) throws XbbException;

    /**
     * 获取采购合同已入库的所有产品信息(用于对采购合同编辑时限制产品数量使用)
     *
     * @param corpid          公司id
     * @param purchaseId 采购合同ID
     * @author Jian.Wang
     * @date 2019/2/21 17:19
     */
    List<PurchaseProductEntity> getAlreadyStoreProduct(String corpid, Long purchaseId) throws XbbException;

    /**
     * 获取采购管理数据
     * @author 吴峰
     * @date 2019/3/4 21:27
     * @param purchaseListDTO
     * @return com.xbongbong.pro.purchase.pojo.vo.PurchaseListVO
     * @throws XbbException 业务异常
     */
    PurchaseListVO findPurchaseList(PurchaseListDTO purchaseListDTO) throws XbbException;

    /**
     * 批量更新采购管理
     * @param purchaseUpdateBatchDTO
     * @return
     * @throws XbbException
     */
    PurchaseUpdateBatchVO updateBatch(PurchaseUpdateBatchDTO purchaseUpdateBatchDTO) throws XbbException;

    /**
     * 禁止调用
     * @param purchaseUpdateBatchDTO
     * @param isUpdateTime
     * @param needThrow
     * @return
     * @throws XbbException
     */
    @Deprecated
    PurchaseUpdateBatchVO updateBatch(PurchaseUpdateBatchDTO purchaseUpdateBatchDTO, Boolean isUpdateTime, boolean needThrow) throws XbbException;

    /**
     * 批量删除
     * @author 吴峰
     * @date 2019/3/7 19:45
     * @param purchaseDeleteBatchDTO
     * @return com.xbongbong.pro.purchase.pojo.vo.PurchaseDeleteBatchVO
     * @throws XbbException 业务异常
     */
    PurchaseDeleteBatchVO deleteBatch(PurchaseDeleteBatchDTO purchaseDeleteBatchDTO) throws XbbException;

    /**
     * 批量插入某个字段
     * @author 吴峰
     * @date 2019/3/20 15:30
     * @param purchaseEditAttrUpdateDTO
     * @return void
     * @throws XbbException 业务异常
     */
    void batchEditAttr(PurchaseEditAttrUpdateDTO purchaseEditAttrUpdateDTO) throws XbbException;

    /**
     * 获取采购合同产品
     *
     * @param businessProductListDTO 入参
     * @param dataList data对象
     * @author chaoxiong.lei
     * @Date 14:37 2019/3/5
     * @since v1.0
     * @return BusinessProductListVO，属性product包含了入库单关联产品，返回给前端的格式
     **/
//   BusinessProductListVO getPurchaseProductList(BusinessProductListDTO businessProductListDTO, JSONObject dataList) throws XbbException;

    /**
     * 汇总采购合同产品信息（详情页，几种商品几件）
     *
     * @param businessProductListDTO 入参
     * @throws XbbException
     * @author long.rao
     * @date 2019-08-28 10:23
     */
    ProductSummaryVO summaryProductList(BusinessProductListDTO businessProductListDTO) throws XbbException;

    /**
     * 根据采购合同id获取采购合同产品的采购价
     *
     * @param corpid 获取采购订单关联产品的条件
     * @param purchaseId 获取采购订单关联产品的条件
     * @return productCostMap   产品id和成本价之间的key-value关系
     * @since v3.22
     * @author lcx
     */
    Map<Long, Double> getReturnedProdCostMap(String corpid, Long purchaseId);

    /**
     * 批量一键采购
     *
     * @param handlerExplainBatchLinkItemDTO
     * @throws XbbException
     */
    void formatBatchLinkAddExplain(HandlerExplainBatchLinkItemDTO handlerExplainBatchLinkItemDTO) throws XbbException;

    /**
     * Description: 关联新建时格式化解释
     * @param handlerExplainInLinkItemDTO
     * @return void
     * @throws XbbException XbbException
     * @author 陈有利
     * @date 2019/5/23 17:22
     * @since v1.0
     */
    void formatLinkAddExplain(HandlerExplainInLinkItemDTO handlerExplainInLinkItemDTO) throws XbbException;

    /**
     * 将Excel数据格式化成表单保存需要的JSON格式的数据
     *
     * @param importFormDataDTO 导入入参
     * @param titlePojoList     标题
     * @param rowGroupPojo      一个表单对应的一组Excel填写的数据
     * @param fieldAttrList     解释
     * @return FormDataAddDTO
     * @throws XbbException XbbException
     * @author youli.chen
     * @date 2019/4/29 14:16
     * @since V1.0
     */
    FormDataAddDTO formatExcelValue(ImportFormDataDTO importFormDataDTO, List<CellTitlePojo> titlePojoList, RowGroupPojo rowGroupPojo, List<FieldAttrEntityForImport> fieldAttrList) throws XbbException;
    /**
     * 格式化导出值
     *
     * @param paasFormDataESList 数据
     * @throws XbbException 异常
     */
    void formatExportValue(List<PaasFormDataEntityExt> paasFormDataESList, FormDataListDTO formDataListDTO, List<FieldAttrEntityForImport> fieldAttrList, Integer businessType) throws XbbException;

    /**
     * 待入库合同维度导出
     *
     * @param paasFormDataESList 数据
     * @throws XbbException 异常
     */
    void formatWaitInstockPurchaseExportValue(List<PaasFormDataEntityExt> paasFormDataESList, FormDataListDTO formDataListDTO) throws XbbException;

    /**
     * 修改采购合同状态
     *
     * @param changeStatusDTO 入参
     * @throws XbbException
     * @author long.rao
     * @date 2019-06-07 14:24
     */
    BaseVO changeStatus(ChangeStatusDTO changeStatusDTO) throws XbbException;

    /**
     * 修改供应商名称
     * @author 吴峰
     * @date 2019/6/20 10:54
     * @param corpid 公司id
     * @param dataId 数据id
     * @param name 供应商名称
     * @return void
     * @throws XbbException 业务异常
     */
    void dataConsistencyUpdateSupplier(String corpid, Long dataId, String name) throws XbbException;

    /**
     * 入库单保存后判断是否出库完毕
     * @param instockId
     * @param refId
     * @param corpid
     * @param productSavePojoList
     * @return
     */
    boolean judgePurchaseAllInBound(Long instockId, Long refId, String corpid, List<ProductSavePojo> productSavePojoList) throws XbbException;

    /**
     *
     * @author 吴峰
     * @date 2019/8/19 13:46
     * @param purchaseUpdateByBusinessRuleDTO
     * @return void
     * @throws XbbException 业务异常
     */
    void updateBatchByBusinessRule(PurchaseUpdateByBusinessRuleDTO purchaseUpdateByBusinessRuleDTO) throws XbbException;

    void formatProduct4Show2(Long dataId, String corpid, JSONObject data, UpdateDataTypeEnum updateDataTypeEnum) throws XbbException;

    PagingProductVO getPagingProduct(PagingProductDTO pagingProductDTO) throws XbbException;

    /**
     * 解析单个产品
     * @param entity
     * @return
     */
    JSONObject formatSingleProduct(PurchaseProductEntity entity);

    /**
     * 更新收款比例
     * @param corpid
     * @param sheetAmount
     * @param purchaseId
     * @param linkContractEntity
     * @throws XbbException
     */
    void updateProfit(String corpid, Double sheetAmount, Long purchaseId, PaasFormDataEntity linkContractEntity) throws XbbException;

    /**
     * 创建付款单时，处理采购合同并封装要更新的结果：判断已收金额是否已经超过合同内的未收金额，并把采购合同的已收金额增加、收款比例重新计算等
     * 已核销、预付款核销：已付金额增加，未付减少，限制付款金额小于采购合同的未付
     *
     * @param sheetTypeEnum 回/付款单类型
     * @param corpid 公司id
     * @param contractIdToAmount 合同id-该合同对应的回款金额
     * @param contractIds 合同ids
     * @return java.util.List<com.xbongbong.pro.domain.entity.UpdateDataEntity>
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    List<UpdateDataEntity> handleContract(SheetTypeEnum sheetTypeEnum, String corpid, Map<Long, Double> contractIdToAmount, Set<Long> contractIds) throws XbbException;

    /**
     * 更新回滚es
     * @param idIn
     * @param corpid
     * @throws XbbException
     */
    void updateRollback(Collection<Long> idIn, String corpid) throws XbbException;

    /**
     * 删除付款单时，同步更新采购合同
     * @param corpid 公司id
     * @param errorTypeSet 不允许删除类型
     * @param errorDataSet 不允许删除数据
     * @param allDelIdList 所有要删除的付款单id集合（因为重置更新付款计划的时间时，需要查询除allDelIdList之外的付款单）
     * @param sheetDeleteHandleRelateDTO 删除/批量删除付款单，更新其关联业务（供应商、采购合同、付款计划）dto
     * @return java.util.List<com.xbongbong.pro.domain.entity.UpdateDataEntity>
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    List<UpdateDataEntity> updateContract4Del(String corpid, Set<String> errorTypeSet, Set<String> errorDataSet, Collection<Long> allDelIdList, SheetDeleteHandleRelateDTO sheetDeleteHandleRelateDTO) throws XbbException;

    /**
     * Description: 获取供应商名称集合
     * @param handoverIdSet
     * @param corpid
     * @author 刘阳
     * @date 2020/1/8 2:41 PM
     * @since v1.0
     */
    Map<Long, String> getPurchaseNameMap(List<Long> handoverIdSet, String corpid) throws XbbException;

    /**
     * 跟新采购合同开启关闭状态
     * @param changeDocumentStatusSaveDTO
     * @return
     */
    ChangeDocumentStatusSaveVO changeDocumentStatus(ChangeDocumentStatusSaveDTO changeDocumentStatusSaveDTO) throws XbbException;

    /**
     * 增删标签
     * @param addLabelDTO
     * @param addBatchList
     * @throws XbbException
     */
    void updateBatchLabelAttr(AddLabelDTO addLabelDTO, List<PaasFormDataEntityExt> addBatchList, Boolean addOrDelete) throws XbbException;

    void afterSaveForWorkflow(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;

    void afterSaveForUpdateWorkflow(SaasFormSaveDTO saasFormSaveDTO) throws XbbException;
}
